import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_page/home_business/home_cell_style/home_section_cell.dart';
import 'package:tiktokadult/hj_page/home_business/home_cell_style/section_exchange_widget.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import '../../../hj_model/home/home_tabs_entity.dart';
import '../../../hj_model/home/module_detail_model.dart';
import '../../../tools_base/banner/ads_grid_view_widget.dart';
import '../../../tools_base/loading/l_error_widget.dart';
import '../../../tools_base/refresh/pull_refresh.dart';
import '../home_cell_style/home_guess_like_sliver.dart';
import '../home_cell_style/home_jg_area_widget.dart';
import '../widget/home_txt_marquee.dart';
import 'home_tab_section_logic.dart';

/// 海角样式
class HomeTabSectionPage extends StatelessWidget {
  final ModuleData tabModel;
  final bool isDarkStyle;
  final int index;

  const HomeTabSectionPage(
    this.index,
    this.tabModel, {
    Key? key,
    this.isDarkStyle = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomeTabSectionLogic>(
      init: HomeTabSectionLogic(index, tabModel, isDarkStyle: isDarkStyle),
      tag: "${tabModel.id}-${DateTime.now().millisecondsSinceEpoch}",
      builder: (logic) {
        return Container(
          child: pullYsRefresh(
            onInit: (ctr) => logic.refreshController = ctr,
            onLoading: (ctr) => logic.loadMoreData(),
            onRefresh: (ctr) => logic.refreshData(),
            child: CustomScrollView(
              controller: logic.scrollController,
              slivers: [
                SliverToBoxAdapter(
                  child: AdsGridViewWidget(
                    4,
                    isBigStyle: true,
                    padding: EdgeInsets.fromLTRB(12, 0, 12, 12),
                  ),
                ),
                SliverToBoxAdapter(child: HomeJgAreaWidget(tabModel)),
                if (logic.isLoadingData)
                  SliverToBoxAdapter(
                    child: Container(
                      height: 300,
                      child: LoadingCenterWidget(),
                    ),
                  )
                else if (logic.allSection.isEmpty)
                  SliverToBoxAdapter(
                    child: Container(
                      height: 300,
                      child: CErrorWidget(
                        retryOnTap: () => logic.initData(),
                      ),
                    ),
                  )
                else
                  ..._buildContent(logic),
              ],
            ),
          ),
        );
      },
    );
  }

  List<Widget> _buildContent(HomeTabSectionLogic logic) {
    return [
      SliverList.builder(
        itemBuilder: (BuildContext context, int index) {
          AllSection section = logic.allSection[index];
          if (section.isAdsArr()) {
            return AdsGridViewWidget(
              -1,
              adsArr: section.adsInfoArr ?? [],
              padding: EdgeInsets.fromLTRB(12, 0, 12, 16),
              horCount: 5.2,
            );
          } else if (logic.lastIsGuessLike && index == logic.allSection.length - 1) {
            return SizedBox(); // 最后一个猜你喜欢列表单独处理
          } else if (section.allVideoInfo?.isNotEmpty == true) {
            return StatefulBuilder(builder: (context, setStates) {
              return Container(
                padding: EdgeInsets.only(bottom: 24),
                child: Column(
                  children: [
                    HomeSectionCell(section),
                    SectionExchangeWidget(
                      item: section,
                      margin: EdgeInsets.fromLTRB(12, 12, 12, 0),
                      callback: () {
                        setStates(() {});
                      },
                    ),
                  ],
                ),
              );
            });
          } else {
            return SizedBox();
          }
        },
        itemCount: logic.allSection.length,
      ),
      if (logic.lastIsGuessLike)
        HomeGuessLikeSliver(
          logic.allSection.last,
          sortIndex: logic.guessLikeSortType,
          tabModel: tabModel,
          logic: logic,
        ),
    ];
  }
}
